/*
 * ReportActionBean.java
 *
 * Created on November 30, 2006, 2:12 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package pkg.action;

import java.io.Serializable;
import javax.cache.Cache;
import javax.servlet.http.HttpSession;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.ValidationError;
import pkg.entity.User;

/**
 *
 * @author will
 */
public class BaseActionBean implements ActionBean {

    private ActionBeanContext context;

    /** Creates a new instance of ReportActionBean */
    public BaseActionBean() {
    }

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext actionBeanContext) {
        this.context = actionBeanContext;
    }

    public void resetUser() {
        HttpSession session = context.getRequest().getSession();
        session.removeAttribute("user");
    }

    public User getUser() {
        HttpSession session = context.getRequest().getSession();
        return (User)session.getAttribute("user");
    }

    public HttpSession getSession() {
        return getContext().getRequest().getSession();
    }

    public void addError(String errorStr) {
        ValidationError error = new SimpleError(errorStr);
        getContext().getValidationErrors().addGlobalError(error);
    }

    public void addError(String field, String errorStr) {
        ValidationError error = new SimpleError(errorStr);
        getContext().getValidationErrors().add(field, error);
    }

    public Object getCache(String key) {
        Cache cache = (Cache) getContext().getServletContext().getAttribute("cache");
        if (cache != null) {
            return cache.get(key);
        }
        return null;
    }

    public void setCache(String key, Serializable o) {
        Cache cache = (Cache) getContext().getServletContext().getAttribute("cache");
        if (cache != null) {
            cache.put(key, o);
        }
    }
}
